<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="上传图片" :visible.sync="dialogVisible"
        @close="resetForm('editPictureSzRef')" width="400px">
        <el-form label-position="top" label-width="auto" size="medium" :hide-required-asterisk='true'
            :model="editPictureSzForm" :rules="editPictureSzRules" ref="editPictureSzRef" v-loading="loading">
            <el-form-item label="上传对象">
                <ul class="sche_info">
                    <li class="flexbox flexcenter flexbetween">
                        <span>品牌/产品</span>
                        <span class="sche_text">{{ brand_name }} - {{ product_name }}</span>
                    </li>
                </ul>
            </el-form-item>
            <el-form-item label="业务场景" prop="business_scene">
                <el-select v-model="editPictureSzForm.business_scene" placeholder="请选择业务场景" style="width:100%">
                    <el-option :label="item.scene_name" :value="item.business_scene" v-for="item in sceneList"
                        :key="item.business_scene"></el-option>
                    <el-option label="短视频原创" :value="1" disabled></el-option>
                    <el-option label="短视频混剪" :value="2" disabled></el-option>
                    <el-option label="直播素材" :value="3" disabled></el-option>
                    <el-option label="直播直投" :value="4" disabled></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="平面" prop="designer_uid" style="margin-bottom:0">
                <el-select v-model="editPictureSzForm.designer_uid" placeholder="请选择平面" style="width:100%">
                    <el-option :label="item.nickname" :value="item.id" v-for="item in graphicList" :key="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resetForm('editPictureSzRef')" class="el-button-width" size="small">取 消</el-button>
            <el-button type="primary" @click="submitForm('editPictureSzRef')" class="el-button-width" size="small"
                :disabled='disabled' :loading="disabled">下一步</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            disabled: false,
            dialogVisible: false,
            brand_name: "",
            product_name: "",
            brand_id: '',
            product_id: '',
            sceneList: [], //业务场景列表
            graphicList:[],//平面-深圳
            is_change:false,
            material:[],
            editPictureSzForm: {
                id: "",
                designer_uid: "",
                business_scene: "",
            },
            isChangeForm:{},
            editPictureSzRules: {
                business_scene: [
                    {
                        required: true,
                        message: "请选择业务场景",
                        trigger: "blur",
                    },
                ],
                designer_uid: [
                    {
                        required: true,
                        message: "请选择平面",
                        trigger: "blur",
                    },
                ],
            },
        };
    },

    created() {
        this.$bus.on("editPictureSz", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.editPictureSzForm.id = value.id;
            this.editPictureSzForm.business_scene = value.business_scene;
            this.editPictureSzForm.designer_uid = value.designer_uid;
            this.brand_name = value.brand_name;
            this.product_name = value.product_name;
            this.brand_id = value.brand_id;
            this.product_id = value.product_id;
            this.material_finish = value.material_finish;
            this.material = value.material;
            this.isChangeForm = JSON.parse(JSON.stringify(value))
            this.getGraph();
            this.getSceneList()
        });
    },
    beforeDestroy() {
        this.$bus.off("editPictureSz");
    },
    methods: {
        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.isChange()) {
                        this.$confirm("需求发生变化，提交后已提交的素材将被清除，确定吗？", "清除", {
                            confirmButtonText: "确认",
                            cancelButtonText: "取消",
                        })
                            .then(() => {
                                this.submitJust()
                            })
                            .catch(() => { });
                    } else {
                        this.submitJust()
                    }

                } else {
                    this.disabled = false;
                    return false;
                }
            });
        },

        isChange(){
            let isChange = false
            if(this.editPictureSzForm.designer_uid!=this.isChangeForm.designer_uid){
                isChange = true
            }else if(this.editPictureSzForm.business_scene!=this.isChangeForm.business_scene){
                isChange = true
            }
            return isChange
        },

        async submitJust() {
            this.disabled = true;
            const { data: res } = await this.$http.post(
                "adminapi/demandsz/pictureAdjust",
                this.editPictureSzForm
            );
            if (res.code !== 200) {
                this.disabled = false;
                this.$message.error(res.msg);
                return;
            } else {
                this.disabled = false;
                this.resetForm('editPictureSzRef');
                this.$parent.getDataList(this.$parent.setpage);
                let attr = {
                    affected_rows:res.data.affected_rows,
                    material:this.material,
                    id:this.editPictureSzForm.id,
                    dialogVsb:true,
                }
                this.$bus.emit("addPictureSz", attr);
            }
        },

        // 获取业务场景
        async getSceneList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/sceneList",
                {
                    brand_id: this.brand_id,
                    product_id: this.product_id,
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.sceneList = res.data;
            }
        },

        // 获取平面
        async getGraph() {
            const { data: res } = await this.$http.post(
                "adminapi/company/szGraphicList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.graphicList = res.data
            }
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
<style scoped>
.sche_info {
    font-size: 14px;
    color: #616c85;
    background-color: rgba(165, 177, 209, 0.08);
    border-radius: 12px;
    padding: 12px;
}

.sche_info li {
    height: 36px;
}

.sche_text {
    color: #17233d;
}
</style>

